<?php
/**
 * 《帐号管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/VpnUtil.class.php');

class User extends VpnUtil {
	public $tbl = Constant::tbl_vpn_user;	
	
	public $c1 = array(
		'-' => '请选择',
		'1' => '不允许',
		'2' => '允许',

		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',
			
		'r2' => '当日剩余流量',
		't2' => '当日总流量',
			
		'r1' => '当月剩余流量',
		't1' => '当月总流量',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'office' => '办公区',
		'dept' => '部门',
		'emp' => '员工',
		'c1' => '同时使用',

		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('c1_option', $this->c1);
	}

	function tab($controller, $request) {
		$data = array(
			'帐号管理' => array('cc'=>'vpn.php?do=User.find', 'js'=>'1'),
			'日志信息' => array('cc'=>'vpn.php?do=Log.find', 'js'=>'1'),
				
			'节点信息' => array('cc'=>'wifi.php?do=Node.find', 'js'=>'1'),
			'访问日志' => array('cc'=>'log.php?do=LogAccess.find', 'js'=>''),
			'员工信息' => array('cc'=>'emp.php?do=Emp.find', 'js'=>'1'),
				
			'统计分析' => array('cc'=>'vpn.php?do=User.tj_tab', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {
		$data = array(
			'帐号管理' => array('cc'=>'vpn.php?do=User.group', 'js'=>'1'),
			'日志信息' => array('cc'=>'vpn.php?do=Log.group', 'js'=>'1'),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tj_tab');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'vpn/user/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _where($request) {
		$name_s = $request['name_s'];
		$c1_s = $request['c1_s'];
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($name_s)) {
			$where .= " AND name = '" . $name_s . "'";
		}
		
		if (!empty($c1_s) && $c1_s <> '-') {
			$where .= " AND c1 = '" . $c1_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'206', '2'=>'207', '3'=>'208');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
				
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}
	
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'vpn/user/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'vpn/user/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$username = $request['username'];
		$password = $request['password'];
		$email = $request['email'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, username, password, email, name, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $username, $password, $email, $name, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'vpn/user/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$username = $request['username'];
		$password = $request['password'];
		$email = $request['email'];

		$ip = $request['ip'];
		
		$quota_cycle = $request['quota_cycle'];
		$quota_bytes = $request['quota_bytes'];
		
		$indate = $request['indate'];
		
		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', username = '%s', password = '%s', ip = '%s', indate = '%s', 
				quota_cycle = '%s', quota_bytes = '%s', name = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $username, $password, $ip, $indate, $quota_cycle, $quota_bytes, $name, $content, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'vpn/user/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);
		
		if (!empty($request['username'])) {
			$row = $this->view_by_username($request['username']);
		} else {

			$id = $request['id'];
			$row = $this->_view($id);
		}

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'vpn/user/view');
		return true;
	}
	
	function view_by_username($username) {
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE username = '%s'";
		$sql = sprintf($fmt, $this->tbl, $username);
		$result = $db->query($sql);
		
		return $db->fetch_array($result);
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row['c1_t'] = $this->c1[$row['c1']];
		
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		$controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));
	
		$num = array(
			'1' => '按次数',
			'2' => '按流量',
		);
		$controller->assign('num_option', $num);
	
		$controller->display($request, 'vpn/user/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'status');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
	
		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(t2) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(t2) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);
	
		$where = $this->_where($request);
	
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
	
		$this->_group($controller, $request);
	
		// $controller->display($request, 'list');
		return true;
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-') {
			$px = 'status';
		} 
		
		if ($px == 'c1') {
			$row['a'] = sprintf("%s [%s]", $this->c1[$row['a']], $row['a']);
		} else {
	
			$row = $this->_name_t($row, $px);
		}
	
		return $row;
	}
	
	function sync($controller, $request) {
	
		$this->sync_r1();
		$this->sync_r2();
		$this->_log();
	}
	
	function sync_r1() {
	
		if (date('d') <> '01') {
			return false;
		}
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET r1 = t1";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function sync_r2() {
	
		$w = date("w");
		
		$db = newdb($this->dsn_w);
		if ($w == '6' || $w == '0') {
			$fmt = "UPDATE %s SET r2 = t2*3";
		} else {
			$fmt = "UPDATE %s SET r2 = t2";
		}
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function _log() {
		require_once(dirname(__FILE__) . '/Connection.class.php');
		$aa = new Connection();
		
		$aa->_log();
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		$s = "sunxiaoye";
		$a = md5($s);
		echo sprintf("str = %s, len = %s\n", $a, strlen($a));
		
		logdate($controller, $request);
		return true;
	}
}
?>
